//
//  WHNLiveCollectionViewCell.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WHNLiveCollectionViewCell.h"
#import "WHNTitleCollectionReusableView.h"
#import "WHNCollectionViewCell.h"
#import "AFTools.h"
#import "WHNLive.h"
#import <UIImageView+WebCache.h>
#import "FitHeader.h"


@implementation WHNLiveCollectionViewCell

#pragma mark 加载图
-(MBProgressHUD *)progressView
{
    if (!_progressView) {
        _progressView = [[MBProgressHUD alloc]initWithView:self];
        _progressView.labelText = @"加载中";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        [_progressView NightWithType:UIViewColorTypeNormal];
    }
    return _progressView;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.WHNLiveCollectionView];
        [self getData];
        [self addSubview:self.progressView];
    }
    return self;
}
#pragma mark - 获取数据
-(void)getData
{
    _WHNData = [NSMutableArray array];
    _WHNRadio = [NSArray arrayWithObjects:@"本地台", @"国家台", @"省市台", @"网络台", nil];
    _WHNPicture = [NSArray arrayWithObjects:@"dingwei.png", @"guojia.png", @"chengshi.png", @"wangluo.png", nil];
    [AFTools getDataByURLString:@"http://live.ximalaya.com/live-web/v1/getHomePageRadiosList?device=android" WithDataBlock:^(id data) {
        NSDictionary *dic = [data objectForKey:@"result"];
        NSArray *reArray = [dic objectForKey:@"recommandRadioList"];
        NSMutableArray *reArr = [NSMutableArray array];
        for (NSDictionary *reDic in reArray) {
            WHNLive *live = [[WHNLive alloc]init];
            [live setValuesForKeysWithDictionary:reDic];
            [reArr addObject:live];
        }
        NSArray *topArray = [dic objectForKey:@"topRadioList"];
        NSMutableArray *topArr = [NSMutableArray array];
        for (NSDictionary *topDic in topArray) {
            WHNLive *live = [[WHNLive alloc]init];
            [live setValuesForKeysWithDictionary:topDic];
            [topArr addObject:live];
        }
        [_WHNData addObject:reArr];
        [_WHNData addObject:topArr];
        [_WHNLiveCollectionView reloadData];
        [_progressView removeFromSuperview];
    }];
}
#pragma mark 懒加载CollectionView初始化
-(UICollectionView *)WHNLiveCollectionView
{
    if (!_WHNLiveCollectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.sectionInset = UIEdgeInsetsMake(10 * FitHeight, 10 * FitWidth, 10 * FitHeight, 10 * FitWidth);
        self.WHNLiveCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];
        self.WHNLiveCollectionView.delegate = self;
        self.WHNLiveCollectionView.dataSource =self;
        [_WHNLiveCollectionView registerClass:[WHNTitleCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
        [_WHNLiveCollectionView registerClass:[WHNTitleCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head1"];
        [_WHNLiveCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"systemHead"];
        [_WHNLiveCollectionView registerClass:[WHNCollectionViewCell class] forCellWithReuseIdentifier:@"one"];
        [_WHNLiveCollectionView registerClass:[WHNCollectionViewCell class] forCellWithReuseIdentifier:@"two"];
        [_WHNLiveCollectionView registerClass:[WHNCollectionViewCell class] forCellWithReuseIdentifier:@"three"];
        [_WHNLiveCollectionView registerClass:[WHNCollectionViewCell class] forCellWithReuseIdentifier:@"four"];
        [_WHNLiveCollectionView NightWithType:UIViewColorTypeNormal];
    }
    return _WHNLiveCollectionView;
}
#pragma mark - collectionView的方法
#pragma mark cell样式
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        WHNCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"one" forIndexPath:indexPath];
        cell.kind = WHNCollectionViewKindOneLabelAndOnePicture;
        cell.mainImageView.layer.masksToBounds = YES;
        cell.mainImageView.layer.borderWidth = 1.0;
        cell.mainImageView.layer.borderColor = [UIColor redColor].CGColor;
        cell.mainImageView.layer.cornerRadius = cell.mainImageView.frame.size.width / 2;
        cell.titleLabel.text = _WHNRadio[indexPath.row];
        cell.mainImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", _WHNPicture[indexPath.row]]];
        return cell;
    }
    if (indexPath.section == 2) {
        WHNCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"two" forIndexPath:indexPath];
        cell.kind = WHNCollectionViewKindOneLabelAndOnePicture;
        if (_WHNData.count != 0) {
            [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [_WHNData[0][indexPath.row] picPath]]] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
            cell.mainImageView.layer.cornerRadius = 5;
            cell.mainImageView.layer.masksToBounds = YES;
            [cell.iconImageView1 NightWithType:UIViewColorType3];
            cell.iconImageView1.layer.cornerRadius = 5;
            cell.iconImageView1.layer.masksToBounds = YES;
            cell.titleLabel.text = [_WHNData[0][indexPath.row] rname];
        }
        return cell;
    }
    if (indexPath.section == 4) {
        WHNCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"three" forIndexPath:indexPath];
        cell.kind = WHNCollectionViewKindThreeLabelAndThreePicture;
        if (_WHNData.count != 0) {
            [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [_WHNData[1][indexPath.row] radioCoverSmall]]]];
            cell.titleLabel.text = [_WHNData[1][indexPath.row] rname];
            cell.byLabel.text = [NSString stringWithFormat:@"正在直播: %@", [_WHNData[1][indexPath.row] programName]];
            cell.iconImageView1.image = [UIImage imageNamed:@"sanjiao1"];
            CGFloat a = [[_WHNData[1][indexPath.row]radioPlayCount]integerValue] / 10000 * 1.0;
            if (a >= 1.0) {
                cell.numLabel.text = [NSString stringWithFormat:@"%.1f万人", a];
            }else {
                cell.numLabel.text = [NSString stringWithFormat:@"%ld人", [[_WHNData[1][indexPath.row]radioPlayCount]integerValue]];
            }
        }
        return cell;
    }

    WHNCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"four" forIndexPath:indexPath];
    cell.kind = WHNCollectionViewKindOneLabel;
    return cell;
}
#pragma mark 每个分区 item 个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 4;
            break;
        case 1:
            return 0;
            break;
        case 2:
            return [_WHNData[0] count];
            break;
        case 3:
            return 0;
            break;
        case 4:
            return [_WHNData[1] count];
            break;
        default:
            break;
    }
    return 0;
}
#pragma mark 头视图大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeMake(0, 0);
    }
    else if (section == 1 || section == 3){
        return CGSizeMake(self.frame.size.width, 10 * FitHeight);
    }
    else
    {
        return CGSizeMake(self.frame.size.width, 30 * FitHeight);
    }
}
#pragma mark 头视图样式
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 2) {
        WHNTitleCollectionReusableView *head =  [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"head1" forIndexPath:indexPath];
        head.WHNLeftImageView.image = [UIImage imageNamed:@"sanjiao"];
        head.WHNHeadLabel.text = @"推荐电台";
        return head;
    }
    else if (indexPath.section == 1 || indexPath.section == 3) {
        UICollectionReusableView *head =  [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"systemHead" forIndexPath:indexPath];
        [head NightWithType:UIViewColorType3];
        return head;
    }
    else if (indexPath.section == 4) {
        WHNTitleCollectionReusableView *head =  [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"head" forIndexPath:indexPath];
        head.WHNLeftImageView.image = [UIImage imageNamed:@"sanjiao"];
        head.WHNHeadLabel.text = @"排行榜";
        head.WHNMoreLabel.text = @"更多";
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(WHNTapAction)];
        head.WHNMoreLabel.userInteractionEnabled = YES;
        [head.WHNMoreLabel addGestureRecognizer:tap];
        head.WHNSmallImageView.image = [UIImage imageNamed:@"xiangyou.png"];
        return head;
    }
    else {
        WHNTitleCollectionReusableView *head =  [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"systemHead" forIndexPath:indexPath];
        head.frame = CGRectMake(0, 0, 0, 0);
        return head;
    }
}
#pragma mark section分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (_WHNData.count == 0) {
        return 0;
    }
    return 5;
}
#pragma mark 每个分区 item 大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(self.frame.size.width / 4 - 20 * FitWidth, 100 * FitHeight);
    }
    else if (indexPath.section == 2) {
        return CGSizeMake(self.frame.size.width / 3 - 15 * FitWidth, 140 * FitHeight);
    }
    else if (indexPath.section == 4) {
        return CGSizeMake(self.frame.size.width, 100 * FitHeight);
    }
    else {
        return CGSizeMake(0, 0);
    }
}
#pragma mark collectionView 点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                [self.delegate passString:[NSString stringWithFormat:@"%ld",indexPath.row] Title:_WHNRadio[0]];
                break;
            case 1:
                [self.delegate passString:[NSString stringWithFormat:@"%ld",indexPath.row] Title:_WHNRadio[1]];
                break;
            case 2:
                [self.delegate passString:[NSString stringWithFormat:@"%ld",indexPath.row] Title:_WHNRadio[2]];
                break;
            case 3:
                [self.delegate passString:[NSString stringWithFormat:@"%ld",indexPath.row] Title:_WHNRadio[3]];
                break;
            default:
                break;
        }
    }

    else if (indexPath.section == 2) {
        self.block([_WHNData[0][indexPath.row]picPath], [_WHNData[0][indexPath.row]radioPlayCount], [_WHNData[0][indexPath.row] radioId], [_WHNData[0][indexPath.row] rname]);
    }
    else if (indexPath.section == 4) {
        self.block([_WHNData[1][indexPath.row]picPath], [_WHNData[1][indexPath.row]radioPlayCount], [_WHNData[1][indexPath.row] radioId], [_WHNData[1][indexPath.row] rname]);
    }
    else {
        
    }
}
#pragma mark - 更多label轻拍方法
-(void)WHNTapAction
{
    self.moreBlock(@"http://live.ximalaya.com/live-web/v1/getTopRadiosList?device=android&radioNum=20");
}
















@end
